package com.minhua.jijiehao2.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.minhua.jijiehao2.constants.DefineKey;
import com.minhua.jijiehao2.mainframe.launcher.loginactivity.impl.LoginActivity;

/**
 * Toast统一管理类
 **/
public class T {
    private static Context mContext;
    private static Toast toast;

    private static String oldMsg;
    private static long oneTime = 0;
    private static long twoTime = 0;
    private static int offset;

    public T() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static void init(Context context) {
        mContext = context;
    }

    public static void showShort(String s) {
        analyseToken(s);
        if (toast == null) {
            offset = Toast.LENGTH_SHORT;
            toast = Toast.makeText(mContext, s, offset);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > offset) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                offset = Toast.LENGTH_SHORT;
                toast.setDuration(offset);
                toast.show();
            }
        }
        oneTime = twoTime;
    }

    private static final String TOKEN_ERROR = "会话失效";
    private static final String SYSTEM_ERROR = "管理员";

    private static String analyseToken(String s) {
        if (s.contains(TOKEN_ERROR)) {
            SPUtils.remove(DefineKey.LOGIN_STATE);
            SPUtils.remove(DefineKey.USER_NAME);
            SPUtils.remove(DefineKey.USER_ID);
            SPUtils.remove(DefineKey.TOKEN);
            Activity activity = ActivityTaskManager.getActivityTaskManager().currentActivity();
            activity.startActivity(new Intent(activity, LoginActivity.class));
            ActivityTaskManager.getActivityTaskManager().finishAllActivityExceptOne(LoginActivity.class);
            s = "";
        } else if (s.contains(SYSTEM_ERROR)) {
            s = "系统异常";
        }
        return s;
    }

    public static void showShort(int resId) {
        showShort(mContext.getString(resId));
    }


    public static void showLong(String s) {
        s = analyseToken(s);

        if (TextUtils.isEmpty(s)) {
            return;
        }
        if (toast == null) {
            offset = Toast.LENGTH_LONG;
            toast = Toast.makeText(mContext, s, offset);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > offset) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                offset = Toast.LENGTH_LONG;
                toast.setDuration(offset);
                toast.show();
            }
        }
        oneTime = twoTime;
    }

    public static void showLong(int resId) {
        showShort(mContext.getString(resId));
    }

}
